package actions;

import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;

import awesomeDocumentIO.AwesomeDocumentExporter;
import awesomeDocumentIO.AwesomeDocumentImporter;
import dialogs.AddSyntaxColorDialog;
import document.AwesomeDocument;
import document.SyntaxHighlighter;

/**
 * Displays the visuals used to get information for a new syntax keyword from the user.
 * @author Brittin
 *
 */
public class AddSyntaxColorMenuAction extends AwesomeMenuAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public AddSyntaxColorMenuAction(String name, String desc) {
		super(name, desc);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// Open the Create New Document Dialog
		AddSyntaxColorDialog dialog = new AddSyntaxColorDialog(this.getParentFrame(e));
		dialog.setVisible(true);
		
		// Get the entered user information and save it
		String[] info = dialog.getEnteredValues();
		if (info == null)
			return;
		System.out.println(info[0]);
		System.out.println(info[1]);
		System.out.println(info[2]);
		if (AwesomeDocumentExporter.exportNewSyntax(info[0], info[1], info[2]))
			JOptionPane.showMessageDialog(null, "Your syntax was succeffully created.");
		else
			JOptionPane.showMessageDialog(null, "There was a problem creating your syntax.");

		// if this document is using the updated doc type, update this document
		if(this.getSelectedTextPanel(e).getDocumentType().getLabel().equals(info[0])) {
			AwesomeDocument doc = AwesomeDocumentImporter.importDocumentType(info[0]);
			this.getSelectedTextPanel(e).setDocumentType(doc);
			SyntaxHighlighter.highlightDocument(this.getSelectedTextPane(e), doc);
		}
	}

}
